<template>
	<view v-if="showupdate2" class="bg-color flex1" :style="'height: ' + divheight +'px'" @touchmove.stop.prevent>
		<image class="update-pic pr" :src="`${$imageBaseUrl}update.png`" mode="widthFix"></image>
		<view class="update-content pr bg-f">
			<view class="update-text flex-R">
				<text class="f44 fb update-title">版本更新</text>
				<view>
					<text class="update-num f18 ml15">v{{msg.versionNumber}}</text>
				</view>
			</view>
			<scroll-view :show-scrollbar="false" v-if="msg.updateNotes" :scroll-y="true" class="update-inr">
				<view><text class="text">{{msg.updateNotes}}</text> </view>
			</scroll-view>
			<view v-if="!showupdate" class="flex-R" style="margin-top: 62rpx;justify-content: space-around;">
				<view @click="cancelupdate" class="btn"><text class="f26">稍后再说</text></view>
				<view @click="startupdate" class="btn btn2"><text class="f26" style="color: #FFFFFF;">立即更新</text></view>
			</view>
			<view v-else style="margin-top: 62rpx;">
				 <progress :percent="percent" activeColor="#e60012"/>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		// props:['msg'],
		data(){
			return{
				divheight:"",
				showupdate:false,
				percent:0,
				showupdate2:false,
				msg: {}
			}
		},
		async created() {
			// #ifdef MP-WEIXIN
			return
			// #endif
			
			this.divheight=uni.getSystemInfoSync().windowHeight
			// 自动更新
			const platform = uni.getSystemInfoSync().platform
			// console.log(platform)
			plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
				const versionCode = widgetInfo.version;
				// console.log('widgetInfo',widgetInfo)    //1.0.7  
				var updateInfo = { //升级检测数据
					"type": platform == 'ios' ? '2' : '1',
					"versionNumber": versionCode,
					"platform": '1'
				}
				uni.$http.create().setConfig({ url: '/apply-version/check-update',data: updateInfo, showLoading: false }).request().then(res => {
					console.log(222222, res)
					//全包更新
					if(res && (res.versionNumber != versionCode)){
						this.msg = res
						this.showupdate2 = true
						// 强制更新
						if(res.forcedUpdating == '1'){
							this.showupdate = true
							this.uploadapp()
						}
					}
				})
				// getApp().$http({
				// 	url: 'user/apply-version/check-update',
				// 	data: updateInfo,
				// 	success: (res)=>{
				// 		// console.log('接口返回的：',res)
				// 		//全包更新
				// 		if(res && (res.version_number != versionCode)){
				// 			this.msg = res
				// 			this.showupdate2 = true
				// 			// 强制更新
				// 			if(res.forced_updating == '1'){
				// 				this.showupdate = true
				// 				this.uploadapp()
				// 			}
				// 		}
				// 	}
				// })
			});
			//更新代码结束
			plus.screen.lockOrientation('portrait-primary');
			const value = uni.getStorageSync('launchFlag');
			if (!value) {
				// launchFlag=true直接跳转到首页
				uni.reLaunch({
					url: '/pages/index/launchpage'
				})
			}else{
				plus.navigator.closeSplashscreen()
			}
		},
		methods:{
			cancelupdate(){
				console.log("取消")
				this.showupdate2=false
			},
			startupdate(){
				console.log("更新")
				this.showupdate=true
				this.uploadapp()
			},
			uploadapp(){
				if (this.msg.forcedUpdating == 0) {
					if (this.msg.downloadUrl) {
						plus.runtime.openURL(this.msg.downloadUrl, error => {
							console.log('openurlfail', error)
						});
					}
				} else {
					// uni.showLoading({
					// 	title: '更新中......',
					// 	mask: true
					// })
					//热更新
					var downloadTask = uni.downloadFile({
						url: this.msg.downloadUrl,
						success: (downloadResult) => {
							console.log('down', downloadResult)
							if (downloadResult.statusCode === 200) {
								plus.runtime.install(downloadResult.tempFilePath, {
									force: false
								}, function() {
									this.showupdate=false
									uni.showToast({
										title: '更新成功！即将重启app',
										icon: 'none'
									})
									uni.hideLoading()
									setTimeout(() => {
										plus.runtime.restart();
									}, 3000)
								}, function(e) {
									uni.showToast({
										title: '更新失败！',
										icon: 'none'
									})
									console.error('install fail...', e);
									uni.hideLoading()
								});
							}
						}
					});
					downloadTask.onProgressUpdate((res) => {
						console.log(res)
						this.percent=res.progress
					});
				}
			},
		}
	}
</script>

<style scoped lang="scss">
.bg-color{background-color: rgba(0,0,0,.5);position: fixed;top: 0;left: 0;width: 750rpx;z-index: 99;align-items: center;justify-content: center;display: flex;flex-direction: column;z-index: 1000;}
.update-pic{width: 630rpx;}
.update-title{color: #2D2A2B;}
.update-content{width: 630rpx;padding:56rpx 40rpx 40rpx 40rpx;border-bottom-left-radius: 30rpx;border-bottom-right-radius: 30rpx; background-color: #FFFFFF;margin-top: -4rpx;}
.update-text{align-items: center;}
.update-num{color: #FFFFFF;background-color: $theme1;border-top-left-radius: 30rpx;border-top-right-radius: 30rpx;border-bottom-right-radius: 30rpx; padding: 0 20rpx;height: 31rpx;text-align: center;line-height: 31rpx;}
.update-inr{max-height: 195rpx;margin-top: 19rpx;}
.text{color: #4D4B4C;font-size: 26rpx;}
.dian{width: 5rpx;height: 5rpx;background-color: #4D4B4C;border-radius: 50%;margin-right:5rpx;}
.btn{color: #666666;border:1px solid #A5A5A5;padding: 23rpx 72rpx;border-radius: 40rpx;}
.btn2{background-color: $theme1;border-color: $theme1;}
</style>
